package com.lkd.handler;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.OrderCheck;
import com.lkd.emq.Topic;
import com.lkd.entity.OrderEntity;
import com.lkd.exception.LogicException;
import com.lkd.service.OrderService;
import com.lkd.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 延迟消息,修改订单状态
 * 下单后10分钟微支付没改无效订单
 */
@Component
@Slf4j
@Topic(TopicConfig.ORDER_CHECK_TOPIC)
public class OrderCheckHandler implements MsgHandler {

    private OrderService orderService;

    @Override
    public void process(String jsonMsg) throws IOException {
        OrderCheck orderCheck = JsonUtil.getByJson(jsonMsg, OrderCheck.class);

        if (StringUtils.isBlank(jsonMsg) || orderCheck == null) {
            throw new LogicException("无效参数");
        }

        orderService.update(Wrappers.<OrderEntity>lambdaUpdate()
                .eq(OrderEntity::getOrderNo, orderCheck.getOrderNo())
                .eq(OrderEntity::getStatus,VMSystem.ORDER_STATUS_CREATE)
                .set(OrderEntity::getStatus, VMSystem.ORDER_STATUS_INVALID)
        );

    }
}
